<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加新业主信息</title>
  </head>
  <body>
    <script type="text/javascript">
      var newHouseWin;
      var editHouseWin;
      Ext.onReady(function(){
        //这里要设置一个变量，用来保存被点中的那个houseId是多少，默认是0
        var houseId = 0 ;
        var buildingType;
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        var buildingUnitTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<div style="float:left; width:90px">{number}</div><div>{name}</div>', '</div></tpl>');
        var buildingUnitStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/building/getAllBuildingUnits"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }, {
              name: 'number'
            }])
        });
        //供应商
        var buildingUnit = new Ext.form.ComboBox({
          width:350,
          store: buildingUnitStore,
          fieldLabel: '楼座单元',
          allowBlank: false,
          blankText: "楼座单元必须选择",
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'building_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择楼座单元...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '90%',
          pageSize: 50,
          //hideTrigger: false,
          //readOnly:true,
          tpl: buildingUnitTemplate,
          minChars: 3,
          editable: true,
          mode: 'remote',
          triggerAction: 'all',
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        buildingUnit.on('select', function(combo, record, index) {
          houseDS.removeAll();
          houseDS.baseParams.action_type = "building";
          houseDS.baseParams.building_id = record.get("id");
          houseDS.load();
          if (this.isExpanded()) {
            this.collapse();
          }
        });

        
        var buildingUnitFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          labelWidth: 55,
          width: '750',
          title: '按单元搜索',
          labelAlign: 'right',
          frame : true,
          url: '/house/create',
          //defaultType: 'textfield',
          items: [{
              layout: 'column',
              border: false,
              items: [buildingUnit]
            }]
        });
        //--------------------------按用户名或用户编号查询----------------------------------------
        var ownerInfoTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{owner}', '</div></tpl>');
        var ownerInfoStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/house/getAllHouseByOwnerInfo"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'owner'
            }, {
              name: 'number'
            }])
        });
        //业主信息
        var ownerInfo = new Ext.form.ComboBox({
          width:350,
          store: ownerInfoStore,
          fieldLabel: '业主信息',
          allowBlank: false,
          blankText: "楼座单元必须选择",
          valueField: 'id',
          displayField: 'owner',
          hiddenName: 'house_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请输入业主信息，比如姓名，编号...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '90%',
          //pageSize: 10,
          //hideTrigger: false,
          //readOnly:true,
          tpl: ownerInfoTemplate,
          minChars: 2,
          editable: true,
          mode: 'remote',
          //triggerAction: 'all',此处没必要显示所有信息
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        ownerInfo.on('select', function(combo, record, index) {
          houseDS.baseParams.house_id = record.get("id");
          houseDS.baseParams.action_type = "house";
          houseDS.load();
          if (this.isExpanded()) {
            this.collapse();
          }
        });

        
        var ownerInfoPanel = new Ext.Panel({
          title: '按用户信息搜索',
          height:50,
          frame:true,
          items:[ownerInfo]
        });

        var searchTabs = new Ext.TabPanel({
          width:450,
          activeTab: 0,
          //frame:true,
          defaults:{autoHeight: true},
          items:[buildingUnitFormPanel,ownerInfoPanel]
        });
        //------------------------------业主Grid--------------------------------
        var houseDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/house/getAllHousesFromAdmin'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            },{
              name: 'building_type',
              mapping:'building.sub_type'
            },{
              name: 'doorplate'
            },{
              name: 'owner'
            },{
              name: 'phone'
            },{
              name: 'id_card'
            },{
              name:'move_in'
            },{
              name:'area'
            },{
              name:'balance'
            },{
              name:'water_pay_year'
            },{
              name:'water_pay_month'
            },{
              name:'lift_pay_year'
            },{
              name:'lift_pay_month'
            },{
              name:'property_pay_year'
            },{
              name:'property_pay_month'
            },{
              name:'is_locked'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        //house的CM
        var houseCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),{
            id: 'common',
            header: '数据库编号',
            dataIndex: 'id',
            width: 30,
            hidden:true
          },{
            header: '编号',
            dataIndex: 'number',
            width: 70
          },{
            header: '建筑类型',
            dataIndex: 'building_type',
            width: 70
          },{
            header: '门牌号',
            dataIndex: 'doorplate',
            width: 70
          },{
            header: '业主姓名',
            dataIndex: 'owner',
            width: 70
          },{
            header: '业主电话',
            dataIndex: 'phone',
            width: 70
          },{
            header: '入住日期',
            dataIndex: 'move_in',
            renderer: Ext.util.Format.dateRenderer('Y-m-d'),
            width: 80
          },{
            header: '面积',
            dataIndex: 'area',
            width: 50
          },{
            header: '余额',
            dataIndex: 'balance',
            width: 50
          },{
            header: '物业费交费日期',
            dataIndex: 'property_pay_year',
            renderer: renderProperty,
            width: 100
          },{
            header: '电梯费交费日期',
            dataIndex: 'lift_pay_year',
            renderer: renderLift,
            width: 100
          },{
            header: '水费交费日期',
            dataIndex: 'water_pay_year',
            renderer: renderWater,
            width: 90
          },{
            header: '锁定状态',
            dataIndex: 'is_locked',
            renderer: renderHouseIsLucked,
            width: 100
          },{
            header: '生成日期',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          },{
            header: '修改日期',
            dataIndex: 'updated_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          }
        ]);
        //render水费日期
        function renderWater(value, cellmeta, record, rowIndex, columnIndex, store){
          return value +"-"+ record.get("water_pay_month");
        }
        //render水费日期
        function renderLift(value, cellmeta, record, rowIndex, columnIndex, store){
          return value +"-"+ record.get("lift_pay_month");
        }
        //render水费日期
        function renderProperty(value, cellmeta, record, rowIndex, columnIndex, store){
          return value +"-"+ record.get("property_pay_month");
        }
        //render水费日期
        function renderHouseIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<font color=red>锁定<font>";
          }else{
            return "<font color=green>未锁定<font>";
          }
        }
        //生成要订单的产品的Grid
        
        var menubar = [{
            text: '添加业主信息',
            iconCls: 'addItem',
            handler: rightMenuNewHouseFn
          }];
        
        var houseGrid = new Ext.grid.GridPanel({
          store: houseDS,
          cm: houseCM,
          loadMask: true,
          //renderTo: 'editor-grid',
          //width: 830,
          height: screenHeight-350,
          //frame: true,
          collapsible: true,
          autoExpandColumn: 'common',
          title: '业主列表',
          frame: false,
          tbar: menubar
        });
        
        //refreshOwnerFeeInfo = refreshOwnerFeeInfo();
        //设置右键菜单
        houseGrid.on('rowcontextmenu', function(housegrid, rowIndex, e){
          houseId = houseDS.getAt(rowIndex).get('id');
          buildingType = houseDS.getAt(rowIndex).get('building_type');
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //top toolbar
        
        //############ 订单-右键菜单 ##########
        var newHouseMenuItem = new Ext.menu.Item({
          text: '【添加业主信息】',
          iconCls: 'addItem',
          handler: rightMenuNewHouseFn
        });
        var editHouseMenuItem = new Ext.menu.Item({
          text: '【编辑业主信息】',
          iconCls: 'editItem',
          handler: rightMenuEditHouseFn
        });
        var lockHouseMenuItem = new Ext.menu.Item({
          text: '【锁定与解锁业主信息】',
          //iconCls: 'album',
          handler: rightMenuLockHousetFn
        });
        
        var rightClick = new Ext.menu.Menu({
          items: [newHouseMenuItem,editHouseMenuItem,lockHouseMenuItem]
        });
        function rightMenuNewHouseFn(grid, rowIndex, e){
          //-----------------------------new water record win---------------------
          newHouseWin = new Ext.Window({
            layout: 'fit',
            width: 487,
            height: 263,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            //modal: true,
            title: '添加新业主信息',
            autoLoad: {
              url: "/house/newHouse",
              scripts: true
            }
          });
          //刷新水费
          newHouseWin.on("destroy",function(){
            houseDS.reload();
          });
          newHouseWin.resizable = false;
          newHouseWin.show();
        }
        function rightMenuEditHouseFn(grid, rowIndex, e){
          //-----------------------------new water recharge record win---------------------
          editHouseWin = new Ext.Window({
            layout: 'fit',
            width: 475,
            height: 263,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            title: '编辑业主信息',
            autoLoad: {
              url: "/house/editHouse?house_id="+houseId,
              scripts: true
            }
          });
          //刷新水费
          editHouseWin.on("destroy",function(){
            houseDS.reload();
          });
          editHouseWin.resizable = false;
          editHouseWin.show();
        }
        
        function rightMenuLockHousetFn(){
          Ext.Ajax.request({
            url : '/house/houseLock',
            success : function (result, request) {
              houseDS.reload();
            },
            failure : function (result, request){
              Ext.MessageBox.alert("提示","通讯失败，请从新操作");
            },
            method : 'POST',
            params : {
              house_id : houseId,
              authenticity_token: authToken.getValue()
            }
          });
        }
        
        //----------------------workbench--------------------------------------
        var findHouseWorkbenchPanel = new Ext.Panel({
          //title: '查找业主',
          //width: 350,
          height:700,
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          layout: {
            type:'vbox',
            align:'stretch'
          },
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          //listeners: {
          //expand: showAgileDiagram
          //},
          items:[searchTabs,houseGrid]
        });

        //在render之前，称把相关grid隐藏
        findHouseWorkbenchPanel.render("house_search");
      })
    </script>
    <div id="house_search"></div>
  </body>
</html>
